<?php
namespace app\admin\controller;

use app\admin\model\Article as model;
use think\Request;

class Article extends Base
{
    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new model();
    }

    public function index($cid = null, $search = '', $top = null, $status = null, $post = null)
    {
        // exit;
        $map = [];

        if ($cid !== null) {
            $map['cid'] = $cid;
            $this->assign('cid', $cid);
        } elseif ($cid === 0) {
            $this->assign('cid', 0);
        } else {
            $this->assign('cid', null);
        }

        if ($search) {
            $map['title'] = ['like', '%' . $search . '%'];
            $this->assign('search', $search);
        }

        if ($top) {
            $map['top'] = $top;
            $this->assign('top', $top);
        }

        if ($status) {
            $map['status'] = $status;
            $this->assign('status', $status);
        }

        if ($post) {
            $map['post'] = $post;
            $this->assign('post', $post);
        }

        $counts_page = 8;
        $counts = $this->model->where($map)->count('id');
        $pages = intval(ceil($counts / $counts_page));

        $data = $this->model->where($map)
            ->field('id,userid,cid,thumb,title,pubtime,sort,status,top,clicks,post,dutyadmin,dutytime')
            ->order('id', 'desc')
            ->paginate($counts_page);

        $data_category = model('Category')->where('status', 1)
            ->where('template', '<>', 0)
            ->field('id, pid, name')
            ->select();

        $data_category = obj_tree($data_category);

        $this->assign('counts', $counts);
        $this->assign('pages', $pages);
        $this->assign('data', $data);
        $this->assign('data_category', $data_category);
        return $this->fetch('index');
    }

    public function add($cid = null)
    {
        if (Request::instance()->isPOST()) {
            $param = input('post.');
            // var_dump($param);
            // exit();
            $title = isset($param['title']) ? trim($param['title']) : '';

            if (empty($title)) {
                $this->error('标题不可为空');
            }
            
            $cid = isset($param['cid']) ? intval($param['cid']) : 0;
            $writer = isset($param['writer']) ? trim($param['writer']) : $this->username;
            $source = isset($param['source']) ? trim($param['source']) : "原创";
            $sort = isset($param['sort']) ? intval($param['sort']) : 0;
            $keywords = isset($param['keywords']) ? trim($param['keywords']) : "";
            $description = isset($param['description']) ? trim($param['keywords']) : "";
            $thumb = isset($param['thumb']) ? trim($param['thumb']) : "";
            $content = isset($param['editorValue']) ? trim($param['editorValue']) : "";
            $top = isset($param['top']) ? intval($param['top']) : 0;
            // $discussed = isset($param['discussed']) ? intval($param['discussed']) : 0;
            // $ismark = isset($param['discussed']) ? intval($param['discussed']) : 0;
            $userid = $this->uid;
            $pubtime = time();

            try {
                $this->model->save([
                    'cid'   => $cid,
                    'title' => $title,
                    'writer' => $writer,
                    'source' => $source,
                    'thumb' => $thumb,
                    'pubtime' => $pubtime,
                    'keywords' => $keywords,
                    'description' => $description,
                    // 'summary' => $summary,
                    'content' => $content,
                    'top'   => $top,
                    // 'discussed' => $discussed,
                    // 'ismark' => $ismark,
                    'sort' => $sort,
                    'userid' => $userid,
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:" . $msg);
            }
            $this->success('添加成功', 'index');
        } else {
            $categories = model('Category')->where('status', 1)->field('id, pid, name')->select();

            if ($categories) {
                $categories = obj_tree($categories);
            }

            $this->assign('cid', $cid);
            $this->assign('categories', $categories);
            return $this->fetch();
        }
    }

    public function edit($id = "")
    {
        $data = $this->model->find($id);

        if (Request::instance()->isPOST()) {
            $param = input('post.');
            // var_dump($param);
            // exit();
            $title = isset($param['title']) ? trim($param['title']) : '';

            if (empty($title)) {
                $this->error('标题不可为空');
            }
            
            $cid = isset($param['cid']) ? intval($param['cid']) : 0;
            $writer = isset($param['writer']) ? trim($param['writer']) : $this->username;
            $source = isset($param['source']) ? trim($param['source']) : "原创";
            $sort = isset($param['sort']) ? intval($param['sort']) : 0;
            $keywords = isset($param['keywords']) ? trim($param['keywords']) : "";
            $description = isset($param['description']) ? trim($param['keywords']) : "";
            $thumb = isset($param['thumb']) ? trim($param['thumb']) : "";
            $content = isset($param['editorValue']) ? trim($param['editorValue']) : "";
            $top = isset($param['top']) ? intval($param['top']) : 0;
            $status = isset($param['status']) ? intval($param['status']) : 0;
            $clicks = isset($param['clicks']) ? intval($param['clicks']) : 0;
            // $discussed = isset($param['discussed']) ? intval($param['discussed']) : 0;
            // $ismark = isset($param['discussed']) ? intval($param['discussed']) : 0;
            $userid = $this->uid;
            $pubtime = time();

            try {
                $this->model->save([
                    'cid'   => $cid,
                    'title' => $title,
                    'writer' => $writer,
                    'source' => $source,
                    'thumb' => $thumb,
                    'pubtime' => $pubtime,
                    'keywords' => $keywords,
                    'description' => $description,
                    // 'summary' => $summary,
                    'content' => $content,
                    'top'   => $top,
                    // 'discussed' => $discussed,
                    // 'ismark' => $ismark,
                    'clicks' => $clicks,
                    'sort' => $sort,
                    'status' => $status,
                    'userid' => $userid,
                ], ['id' => $id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $msg = substr($msg, 0, 15);

                $this->error("错误提示:" . $msg);
            }
            $this->success('编辑成功', 'index');
        } else {
            $categories = model('Category')->where('status', 1)->field('id, pid, name')->select();

            if ($categories) {
                $categories = obj_tree($categories);
            }

            $this->assign('categories', $categories);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /**
     * 置顶 top
     */
    public function top($id, $value)
    {
        if (Request::instance()->isAjax()) {
            // var_dump(input('post.'));
            // exit;
            $result = $this->model->save([
                'top' => $value,
            ], ['id' => $id]);
            if ($result === false) {
                $this->error('操作失败');
            } else {
                return ["code" => 2, 'msg' => '操作成功'];
            }
        }
    }

    /**
     * 文章审核
     *
     * @param integer $id
     * @param integer $ispost
     * @return void
     */
    public function duty($id = '', $post = 0)
    {
        if (Request::instance()->isAjax()) {
            $time = time();

            $data = $this->model->get($id);

            $data->post = $post;

            $data->dutyadmin = $this->uid;

            $data->dutytime = $time;

            $date_time = date('Y-m-d H:i:s', $time);

            if ($data->save()) {
                return ['code' => 2, 'msg' => '审核成功', 'dutyadmin' => $this->uid, 'time' => $date_time];
            } else {
                return ['code' => 0, 'msg' => '审核失败'];
            }
        }
    }

    /**
     *
     * 预览文章
     *
     */
    public function preview($id = '')
    {
        $field = 'id, cid, title, source, writer, keywords, description,content, pubtime,clicks';

        $data = $this->model->field($field)->find($id);

        $this->assign('data', $data);

        return $this->fetch();
    }

    public function checklist($cid = null)
    {
        $map['ispost'] = ['<>', 1];

        $counts_page = 3;

        $counts = $this->model->where($map)->count('id');

        $pages = intval(ceil($counts / $counts_page));

        $field = "id, thumb, title, pubtime, ispost";

        $data = $this->model->where($map)->field($field)->order('id', 'desc')->paginate($counts_page);

        $this->assign('counts', $counts);

        $this->assign('pages', $pages);

        $this->assign('data', $data);

        return $this->fetch();
    }
}
